{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba142b5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import date\n",
    "from gs_quant.common import PayReceive, Currency\n",
    "from gs_quant.instrument import CDIndex\n",
    "from gs_quant.risk import CDATMSpread\n",
    "from gs_quant.session import Environment, GsSession\n",
    "from gs_quant.markets import PricingContext\n",
    "from gs_quant.markets.portfolio import Portfolio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84b1dfa3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "378e5921",
   "metadata": {},
   "source": [
    "Rolling a position on XOVER from S34 to S35"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c17f3f58",
   "metadata": {},
   "outputs": [],
   "source": [
    "xover_34 = CDIndex(index_family='iTraxx Europe XOVER', index_series=34, buy_sell='Sell', termination_date='5y', name='sell34')\n",
    "xover_35 = CDIndex(index_family='iTraxx Europe XOVER', index_series=35, buy_sell='Buy', termination_date='5y', name='buy35')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a017b8d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "xover_roll = Portfolio((xover_34, xover_35))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aced6560",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "with PricingContext(market_data_location='LDN'):\n",
    "    roll_spreads = xover_roll.calc(CDATMSpread)\n",
    "    roll_price = xover_roll.price()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "465eb4ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = roll_spreads.to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5698ddf",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.loc['buy35'] - df.loc['sell34']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77f522ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "roll_price.aggregate()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
